package com.shopping.servlet;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.shopping.bean.User;
import com.shopping.service.UserService;
import com.shopping.service.impl.UserServiceImpl;

/**
 * @author zjh
 *
 */
@WebServlet("/register.do")
public class RegisterServlet extends HttpServlet {
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String phone = request.getParameter("phone");
		UserService us= new UserServiceImpl();
		Date regdate = new Date();
		String userSex = "男";
		String userImage = "images/default_avatar.jpg";
		User u = new User(username,password,phone,regdate,userImage,userSex);
		int regNum = us.addUser(u);
		if(regNum == 1) {
			request.setAttribute("regmsg", "注册成功");
			request.getRequestDispatcher("login.jsp").forward(request, response);
		}else if(regNum == -1) {
			request.setAttribute("regmsg", "用户名已存在");
			request.getRequestDispatcher("register.jsp").forward(request, response);
		}else {
			request.setAttribute("regmsg", "注册失败");
			request.getRequestDispatcher("register.jsp").forward(request, response);
		}
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		doGet(request, response);
	}

}
